package com.louzhu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.louzhu.gulimall.product.entity.AttrEntity;
import com.louzhu.gulimall.product.service.AttrAttrgroupRelationService;
import com.louzhu.gulimall.product.service.AttrService;
import com.louzhu.gulimall.product.service.CategoryService;
import com.louzhu.gulimall.product.vo.AttrGroupRelationVo;
import com.louzhu.gulimall.product.vo.AttrGroupWithAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.louzhu.gulimall.product.entity.AttrGroupEntity;
import com.louzhu.gulimall.product.service.AttrGroupService;
import com.louzhu.common.utils.PageUtils;
import com.louzhu.common.utils.R;


/**
 * 属性分组
 *
 * @author jianfneg
 * @email 501039356@qq.com
 * @date 2021-07-12 15:03:31
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params , @PathVariable Long catelogId){

//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		// 用当前分类ID查询出所有父级分类ID
        Long[] path = categoryService.finCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**.
     * 获取属性分组关联的所有属性
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrEntities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",attrEntities);
    }

    /**
     * 添加属性分组与属性关联
     */
    @PostMapping("attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        relationService.saveBatch(vos);
        return R.ok();
    }

    /**
     * 删除属性分组与属性关联
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrGroupRelationVo> vos){
        relationService.deleteBatch(vos);
        return R.ok();
    }

    /**
     * 获取属性分组尚未关联的其他属性
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R atterNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                             @RequestParam Map<String,Object> params){
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);

    }


    /**
     * 获取某个三级分类下的所有属性分组及其关联的属性列表
     * 查出分类关联的所有属性分组
     * 查出属性分组关联的所有属性
     * /product/attrgroup/225/withattr
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrVo> attrGroupWithAttrVos = attrGroupService.getAttrGroupWithAttrByCatelogId(catelogId);
        return R.ok().put("data",attrGroupWithAttrVos);
    }



}
